home *** CD-ROM | disk | FTP | other *** search
- Path: news.iadfw.net!usenet
- From: Mark Nelson <markn@airmail.net>
- Newsgroups: comp.lang.c++
- Subject: Re: STL
- Date: Wed, 21 Feb 1996 08:36:31 -0600
- Organization: customer of Internet America
- Message-ID: <312B2DEF.65C7@airmail.net>
- References: <4gdgt0$494@netaxs.com>
- NNTP-Posting-Host: dal14-23.ppp.iadfw.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
-
- Bob Pesavento wrote:
- >
- > I'm a newbie with STL and not an expert at C++ so bear with me. Assume a
- > simplified class:
- >
- > class myclass
- > {
- > int myint;
- > float myfloat;
- >
- > public:...
- >
- > I want use "list" and stuff a bunch of these in using push_back. No
- > problem so far. But I want the user to be able to select which item to
- > sort on... myint or myfloat.
-
- Hi Bob,
-
- The sorting is done using operator<(), so you don't have much choice about that.
- In your case, I would create a static class member that defined which sort
- of sorting you wanted (excuse any syntax errors, this is on they fly):
-
- class myclass {
- public :
- static enum {
- SORT_ON_INT,
- SORT_ON_FLOAT } kind;
-
- Then, in operator<(), you do this:
-
- if ( kind == SORT_ON_INT )
- return myint < rhs.myint;
- else
- return myfloat < rhs.myfloat.
-
- The catch behind all this is that you had better set myclass::kind
- to the proper value before you insert anything into your map or
- set. For the list<>, before you sort.
-
- Mark Nelson
- http://web2.airmail.net/markn -- more STL pointers
-